﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Forms;

namespace MadPropsDemo.Tests.RecipeEditorTests
{
    [TestClass]
    public class when_the_user_clicks_Load : RecipeEditorTestBase
    {
        // TODO should show a "Really discard unsaved changes?" dialog

        [TestMethod]
        public void ok_path()
        {
            // arrange
            RecipeName = "don't care";
            Assert.IsTrue(HasUnsavedChanges);

            fakeDialogService.ReturnValue = DialogResult.OK;

            var dto = new RecipeEditor.Model.RecipeDTO() { Name = "stew" };
            fakeRecipeService.Load_ReturnValue = dto;

            // act
            viewModel.Commands.Load.Execute(null);

            // an OpenFileDialog was shown
            Assert.AreEqual(1, fakeDialogService.TimesCalled);
            var dialog = (OpenFileDialog)(fakeDialogService.CapturedDialog);

            // the initial filename was empty
            Assert.AreEqual("", dialog.FileName);

            // and the filter was correct
            Assert.AreEqual(ExpectedDialogFilter, dialog.Filter);

            // the user clicked OK

            // the recipe service was called
            Assert.AreEqual(1, fakeRecipeService.Load_TimesCalled);

            // and the return value is the new recipe
            Assert.AreEqual("stew", RecipeName);

            // the unsaved changes flag is cleared
            Assert.IsFalse(HasUnsavedChanges);
        }

        [TestMethod]
        public void cancel_path()
        {
            // arrange
            RecipeName = "burritos";
            Assert.IsTrue(HasUnsavedChanges);

            fakeDialogService.ReturnValue = DialogResult.Cancel;

            // act
            viewModel.Commands.Load.Execute(null);

            // an OpenFileDialog was shown
            Assert.AreEqual(1, fakeDialogService.TimesCalled);
            var dialog = (OpenFileDialog)(fakeDialogService.CapturedDialog);

            // the initial filename was empty
            Assert.AreEqual("", dialog.FileName);

            // and the filter was correct
            Assert.AreEqual(ExpectedDialogFilter, dialog.Filter);

            // the user clicked Cancel

            // the recipe service was not called
            Assert.AreEqual(0, fakeRecipeService.Load_TimesCalled);

            // the Unsaved Changes flag remains set
            Assert.IsTrue(HasUnsavedChanges);
        }
    }
}
